package com.example.controller;


import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.entity.dto.AddGuzhangtypeDto;
import com.example.entity.dto.SelGuzhangTypeDto;
import com.example.entity.dto.UpdateGuZhangTypeDto;
import com.example.entity.yifei.Guzhanglog;
import com.example.entity.yifei.Guzhangtype;
import com.example.mapper.GuzhanglogMapper;
import com.example.mapper.GuzhangtypeMapper;
import com.example.service.GuzhangtypeService;
import com.example.untils.BeanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lan
 * @since 2024-12-19
 */
@RestController
@Api(tags = "故障类型")
@RequestMapping("/api/guzhangtype")
public class GuzhangtypeController {
    @Autowired
    private GuzhangtypeService guzhangtypeService;

    @Autowired
    private GuzhangtypeMapper guzhangtypeMapper;
    @Autowired
    private GuzhanglogMapper guzhanglogMapper;

    //新增故障类型
    @ApiOperation("新增故障单")
    @PostMapping("addGuZhangType")
    @DSTransactional
    public Result addGuZhangType(@RequestBody AddGuzhangtypeDto addGuzhangtypeDto) {
        if (addGuzhangtypeDto.getGuzhangtypeno() == null){
            throw new RuntimeException("故障类型编号不能为空");
        }
        Guzhangtype guzhangtype = new Guzhangtype();
        BeanUtils.copyPropertiesIgnoreCase(addGuzhangtypeDto, guzhangtype);
        guzhangtype.setModitime(addGuzhangtypeDto.getCreatetime());
        guzhangtype.setModifier(addGuzhangtypeDto.getCreator());

        LambdaQueryWrapper<Guzhangtype> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Guzhangtype::getGuzhangtypeno, addGuzhangtypeDto.getGuzhangtypeno());
        Integer integer = guzhangtypeMapper.selectCount(wrapper);
        if (integer>0){
            return Result.error("故障类型编号已存在");
        }
        guzhangtypeMapper.insert(guzhangtype);
        return Result.success();
    }

    //删除故障类型
    @DeleteMapping("/deleteGuZhangType/{id}")
    @ResponseBody
    @ApiOperation("删除故障类型")
    public Result deleteGuZhangType(@PathVariable Long id){
        //如果在guzhanglog中有的故障编号不能删除
        LambdaQueryWrapper<Guzhanglog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Guzhanglog::getGuZhangTypeNo,id);
        if (guzhanglogMapper.selectCount(wrapper)>0){
            throw new RuntimeException("故障类型已被使用，不能删除");
        }
        guzhangtypeMapper.deleteById(id);
        return Result.success();
    }

    //修改故障类型
    @ApiOperation("修改故障类型")
    @PostMapping("updateGuZhangType")
    public Result updateGuZhangType(@RequestBody UpdateGuZhangTypeDto updateGuZhangTypeDto) {
        //如果在guzhanglog中有的故障编号不能修改
        LambdaQueryWrapper<Guzhanglog> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Guzhanglog::getGuZhangTypeNo,updateGuZhangTypeDto.getGuZhangTypeNo());
        if (guzhanglogMapper.selectCount(wrapper)>0){
            throw new RuntimeException("故障类型已被使用，不能修改");
        }
        Guzhangtype guzhangtype = new Guzhangtype();
        BeanUtils.copyPropertiesIgnoreCase(updateGuZhangTypeDto, guzhangtype);
        guzhangtypeMapper.updateGuZhangType(guzhangtype);
        return Result.success();
    }

    //查询故障类型
    @ApiOperation("查询故障类型")
    @PostMapping("selectGuZhangType")
    public Result<List<Guzhangtype>> selectGuZhangType(@RequestBody SelGuzhangTypeDto selGuzhangTypeDto) {
        List<Guzhangtype> list = guzhangtypeMapper.selectGuZhangType(selGuzhangTypeDto);
        return Result.success(list);
    }

}
